/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jforum.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jforum.DAO.MessageDAO;
import jforum.domain.User;
import jforum.util.AuthModule;
import jforum.util.DBConnector;

/**
 *
 * @author Вадим
 */
public class DeleteMessageAction implements Action {

    public String perform(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user=AuthModule.getUser(request.getCookies());
        if (user!=null) {
            if (user.getAdminRights()) {
                String str_id = request.getParameter("id");
                int id=-1;
                try {
                    id=Integer.parseInt(str_id);
                } catch (NumberFormatException ex) {
                    request.getSession().setAttribute("errorMessage","Id сообщения должно быть натуральным числом.");
                    return ActionNames.error_page;
                }
                DBConnector conn = DBConnector.getInstance();
                MessageDAO dao = new MessageDAO(conn);
                dao.Delete(id);

                return ActionNames.view_messages_action;
            }
        }
        request.getSession().setAttribute("errorMessage", "Авторизуйтесь чтобы войти.");
        return ActionNames.error_page;
    }
}
